package cn.michael.dev.demo;

/**
 * Created by hufenggang on 2021/11/4.
 */
public class Demo {

    /**
     * 合并两个有序数组
     * -- 采用双指针方式合并两个有序数组
     *
     * @param nums1
     * @param nums2
     */
    public int[] mergeTowArrays(int[] nums1, int[] nums2) {
        int p1 = 0, p2 = 0;
        int m = nums1.length;
        int n = nums2.length;
        int[] newArray = new int[m + n];
        int curr;

        while (p1 < m || p2 < n) {
            if (p1 == m) {
                curr = nums2[p2++];
            } else if (p2 == n) {
                curr = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                curr = nums1[p1++];
            } else {
                curr = nums2[p2++];
            }

            newArray[p1 + p2 - 1] = curr;
        }

        return newArray;
    }
}
